from django.shortcuts import render
from .models import Article
from django.views.decorators.http import require_http_methods, require_GET, require_POST
from django.http import HttpResponse


@require_GET
def index(request):
    articles = Article.objects.all()
    return render(request, "index.html", context={"articles": articles})


@require_http_methods(['POST', 'GET'])
def add_article(request):
    if request.method == "GET":
        return render(request, "add.html")
    if request.method == "POST":
        name = request.POST.get('name')
        content = request.POST.get('content')
        Article.objects.create(name=name, content=content)
        return HttpResponse("success")
